package com.nice.nice201110.service;

import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.entity.TMedicationTreatment;
import com.nice.nice201110.entity.TNiceFile;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.mapper.TNiceFileMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.utils.UploadFile;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TniceFileService {

    @Autowired
    TNiceFileMapper tNiceFileMapper;

    @Autowired
    TLogMapper tLogMapper;

    private TLog tLog = new TLog();
    private List<String> fileNameList = new ArrayList<String>();
    private String filePath = "";

    /** 单一查询  */
    public TNiceFile getOne(UserVo userVo, Integer id){
        TNiceFile tNiceFile = tNiceFileMapper.selectByPrimaryKey(id);
        if(tNiceFile!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tNiceFile.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_nice_file);
            tLog.setRemark("查看文件信息:"+tNiceFile.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tNiceFile;
    }
    /** 插入信息  */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public List<String> insert(UserVo userVo, MultipartFile[] files){
        UploadFile uploadFile = new UploadFile();
        try {
             for(MultipartFile file:files){
                TNiceFile tNiceFile = uploadFile.uploadFile(file, filePath, "");
                if(tNiceFile!=null){
                    tNiceFile.setUserid(Integer.parseInt(userVo.getId()));
                    Integer tNiceFileNum = tNiceFileMapper.insertSelective(tNiceFile);
                    fileNameList.add(tNiceFile.getId().toString());
                    if(tNiceFileNum>0){
                        tLog.setDate(new Date());
                        tLog.setType(0);  //操作类型（新增0，修改1，删除2,查询3）
                        tLog.setDocid(tNiceFile.getId());
                        tLog.setUsername(userVo.getName());
                        tLog.setUserid(Integer.valueOf(userVo.getId()));
                        tLog.setTtable(NiceTableEnum.form_nice_file);
                        tLog.setRemark("插入文件信息:"+tNiceFile.toString());
                        tLogMapper.insertSelective(tLog);
                    }else{
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return null;
                    }
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return null;
                }
             }
        }catch(Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }
        return fileNameList;
    }
}
